package cn.stylefeng.guns.modular.web.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.core.ParamUtil;
import cn.stylefeng.guns.modular.web.entity.GiftType;
import cn.stylefeng.guns.modular.web.service.IGiftTypeService;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * 描述：礼品分类表 controller
 * 作者：chenhuiyan
 * 时间：2020-04-26
 */
@Controller
@RequestMapping("/web/giftType")
public class GiftTypeController extends BaseController {

    private String PREFIX = "/modular/web/giftType/";

    @Autowired
    private IGiftTypeService giftTypeService;

    /**
     * 跳转到管理首页
     */
    @RequestMapping(value = "")
    public String index() {
        return PREFIX + "giftType.html";
    }

    /**
     * 获取列表
     */
    @ResponseBody
    @RequestMapping(value = "/list")
    public Object list() {
        Map<String, Object> params = ParamUtil.getParams();
        Page<Map<String, Object>> list = giftTypeService.list(params);
        return LayuiPageFactory.createPageInfo(list);
    }

    /**
     * 跳转添加/修改页面
     * @param giftTypeId
     * @param model
     * @return
     */
    @RequestMapping(value = "/addOrEdit")
    public String addOrEdit(String giftTypeId, Model model) {
        model.addAttribute("giftTypeId", giftTypeId);
        return PREFIX + "giftType_add.html";
    }

    /**
     * 保存
     * @param giftType
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/save")
    public ResponseData save(GiftType giftType) {
        if (ToolUtil.isEmpty(giftType.getId())) {
            //判断级别是否存在
            GiftType gt = giftTypeService.getOne(new QueryWrapper<GiftType>()
                    .eq("name", giftType.getName())
                    .eq("del_flag", 0));
            if(ToolUtil.isNotEmpty(gt)){
                throw new ServiceException(BizExceptionEnum.EXISTED_GIFT_TYPE);
            }

            giftType.preInsert();
            giftTypeService.save(giftType);
        }else{
            giftType.preUpdate();
            giftTypeService.updateById(giftType);
        }
        return SUCCESS_TIP;
    }

    /**
     * 获取详情
     * @param giftTypeId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getInfoById/{giftTypeId}")
    public Object getInfoById(@PathVariable("giftTypeId") String giftTypeId){
        GiftType giftType = giftTypeService.getById(giftTypeId);
        return giftType;
    }

    /**
     * 删除
     * @param giftTypeId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete")
    public ResponseData delete(@RequestParam(name = "giftTypeId") String giftTypeId){
        if(ToolUtil.isEmpty(giftTypeId)){
            throw new ServiceException(BizExceptionEnum.PARAM_ERROR);
        }
        GiftType giftType = giftTypeService.getById(giftTypeId);
        giftType.preUpdate();
        giftType.setDelFlag(1);
        giftTypeService.updateById(giftType);
        return SUCCESS_TIP;
    }

}
